﻿@page "/CreatCKD"
@inject CKDServer cKDServer
@using System.ComponentModel
@using AntDesign.TableModels
@using Microsoft.AspNetCore.Http
@inject ModalService _modalService
@inject MessageService _message
@inject IHttpContextAccessor httpContextAccessor
<h3>出库扫码验证</h3>
<div>

    @* <Row>
    <Col Span="24">
    <Select style="width:100%" DataSource="@DataList"
    AllowClear="true"
    ValueOnClear="@("")"
    @bind-Value="@select"
    ValueName="@nameof(XSCKDLIST.FBILLNO)"
    LabelName="@nameof(XSCKDLIST.FBILLNO)">
    </Select>
    </Col>
    </Row>*@
    <Row>
        <Row>
            <Col Span="24" Style="font-size:larger">请选择客户</Col>
        </Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@FCustomerNames"
                AllowClear="true"
                ValueOnClear="@(null)"
                @bind-Value="@selectCustomerName"
                ValueName="@nameof(XSCKD.FCustomerName)"
                LabelName="@nameof(XSCKD.F_YHKJ_ZJM)"
                TItem="XSCKD"
                TItemValue="string"
                OnSelectedItemChanged="CustormerSelectChange"
                IgnoreItemChanges=false>
        </Select>
        </Col>

    </Row>
    <Row>
        <Col Span="24">
        <Input style="width:100%" Placeholder="客户名称" @bind-Value="@selectCustomerName" />
        </Col>
    </Row>

    <Row>
        <Row>
            <Col Span="24" Style="font-size:larger">请选择物流</Col>
        </Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@F_YHKJ_WLGSs"
                AllowClear="true"
                ValueOnClear="@(null)"
                @bind-Value="@selectF_YHKJ_WLGS"
                ValueName="@nameof(XSCKD.F_YHKJ_WLGS)"
                LabelName="@nameof(XSCKD.F_YHKJ_WLGS)"
                TItem="XSCKD"
                TItemValue="string"
                OnSelectedItemChanged="F_YHKJ_WLGSSelectChange"
        IgnoreItemChanges=false>
        </Select>
        </Col>
    </Row>

    @*<Row>
        <Row>
            <Col Span="24" Style="font-size:larger">存在单据</Col>
        </Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@FBILLNOs"
                AllowClear="true"
                ValueOnClear="@(null)"
                @bind-Value="@selectFBILLNO"
                ValueName="@nameof(XSCKD.FBILLNO1)"
                LabelName="@nameof(XSCKD.FBILLNO1)"
                TItem="XSCKD"
                TItemValue="string"
                
                IgnoreItemChanges=false>
        </Select>
        </Col>
    </Row>*@

    <Row>
        <Col Span="24" Style="font-size:larger">扫码输入框</Col>
    </Row>
    <Row>
        <Col Span="24">
            @* <Input @ref="@input" style="width:100%" Placeholder="Basic usage" @bind-Value="@ss" @onkeydown="@Change" /> *@
               <input @ref="input" Type="text" style="width:100%;" placeholder="Basic usage" @bind:event="oninput" @bind="@ss" @onkeydown="Change" />

       </Col>
    </Row>
    <Col Span="24" Style="font-size:larger">批号</Col>
    <Col Span="24">
    <Input style="width:100%" Placeholder="批号" @bind-Value="@Batch" />
    </Col>


    <Row>
        <Col style="width:70%">
        <Input Placeholder="物料编码" @bind-Value="@MaterialCode" />
        </Col>

        <Col style="width:30%">
        <Input Placeholder="重量" @bind-Value="@Weight" />
        </Col>
    </Row>
    @*<Row>
    <Col Span="24">扫码输入框</Col>
    </Row>
    <Row>
    <Col Span="24">
    <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@ss" @onkeydown="@Change" />
    </Col>
    </Row>
    <br>
    <Row>

    <Col style="width:70%">
    <Input Placeholder="Basic usage" @bind-Value="@MaterialCode" />
    </Col>

    <Col style="width:30%">
    <Input Placeholder="Basic usage" @bind-Value="@Weight" />
    </Col>
    </Row>
    <Row>

    </Row>
    <Row>
    <Col Span="24">批号</Col>
    <Col Span="24">
    <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@Batch" />
    </Col>
    <Col Span="24">客户名称</Col>
    <Col Span="24">
    <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@FCustomerName" />
    </Col>
    <Col Span="24">物流公司</Col>
    <Col Span="24">
    <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@WLGS" />
    </Col>*@
    <Row>
        
       @* <Col Span="12">
        <label style="font-size:larger">单据总数：</label>
        <label style="font-size:larger">@FBillAmount</label>
        </Col>
        <Col Span="12">
         <label style="font-size:larger">单据未扫：</label>
        <label style="font-size:larger">@FBillSurplus</label>
        </Col>*@
        <Col Span="24">
        <label style="font-size:larger">总数：</label>
        <label style="font-size:larger">@Amount</label>
        <label style="font-size:larger">未扫：</label>
        <label style="font-size:larger">@Surplus</label>
        </Col>
    </Row>


    <Row>

    </Row>
    <Row>
        <Col Span="24">
            @* <Button style="width:100%" Type="@ButtonType.Primary">解析二维码</Button>
        <br>*@
        <br>
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">提交</Button>
        <br>
        </Col>
    </Row>
</div>

@code {
    string Batch { get; set; }
    string ss { get; set; }
    string Result { get; set; }
    string MaterialCode { get; set; }
    string Weight { get; set; }
    string WLGS { get; set; }
    private ElementReference input;
    //物料列表
    ObservableCollection<XSCKD> MaterialList { get; set; } = new ObservableCollection<XSCKD>();
    //客户列表
    ObservableCollection<XSCKD> FCustomerNames { get; set; } = new ObservableCollection<XSCKD>();
    //物流列表
    ObservableCollection<XSCKD> F_YHKJ_WLGSs { get; set; } = new ObservableCollection<XSCKD>();
    //单据列表
    ObservableCollection<XSCKD> FBILLNOs { get; set; } = new ObservableCollection<XSCKD>();
    //选择的客户
    string selectCustomerName;
    //选择的物流
    string selectF_YHKJ_WLGS;
    //选择的单据
    string selectFBILLNO;
    int Amount { get; set; }
    //剩余未扫
    int Surplus { get; set; }
    //当前单据总数
    int FBillAmount { get; set; }
    //当前单据未扫
    int FBillSurplus { get; set; }
    public ObservableCollection<XSCKD> DataList = new ObservableCollection<XSCKD>();
    public string select;
    protected override async Task OnInitializedAsync()
    {
        Result = "";
        var materialList = await cKDServer.GetXSCKDS();
        if (materialList != null)
        {
            foreach (var item in materialList)
            {
                MaterialList.Add(item);
            }
        }

        //客户集合
        var customerNameList = materialList.Where(e => e.FEntrynote != "出库检验扫码").GroupBy(e => e.FCustomerName).Select(e => e.First());
        if (customerNameList != null)
        {
            foreach (var item in customerNameList)
            {
                FCustomerNames.Add(item);
            }
        }

        base.OnInitialized();
    }
    private async Task Change(KeyboardEventArgs e)//KeyboardEventArgs e
    {
        try
        {
            
            if (e.Key == "Enter" )// if (e.Key == "Enter" && e.Code == "Enter")
            {
                if (!string.IsNullOrEmpty(ss))
                {

                    var a = ss.Split(',');
                    Batch = a[0];
                    MaterialCode = a[1];
                    Weight = a[2];
                }

                //var Bill = lists.FirstOrDefault(e => e.Flot != null && e.Flot.StartsWith(Batch));
                //if (Bill != null)
                //{
                //    WLGS = Bill.F_YHKJ_WLGS;
                //    var a = Bill.FEntrynote.Length;
                //    FCustomerName = string.Format($"{Bill.FCustomerName}:{Bill.FBILLNO1}");
                //    Amount = lists.Count(e => e.F_YHKJ_WLGS == Bill.F_YHKJ_WLGS && e.FCustomerName == Bill.FCustomerName);
                //    Surplus = lists.Count(e => e.F_YHKJ_WLGS == Bill.F_YHKJ_WLGS && e.FCustomerName == Bill.FCustomerName && e.FEntrynote != "出库检验扫码");
                //}

                //else
                //{
                //    _message.Info("未找到该批号的信息");
                //}
            }

        }
        catch (Exception)
        {


        }
    }
    private async Task Creat()
    {
        await _modalService.ConfirmAsync(new ConfirmOptions()
            {
                Title = "提示",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            OkText = "确认",
            OnOk = async (e) =>
            {

                var hc = httpContextAccessor.HttpContext;
                var employee = hc.User.FindFirst("employeeName").Value;
                var aa = await cKDServer.UpdatCKD(Batch, selectCustomerName, selectF_YHKJ_WLGS, employee);
                if (aa == "OK")
                {
                    var materialList = await cKDServer.GetXSCKDS();
                    Amount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS);
                    Surplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码");
                    //    FBillAmount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FBILLNO1 == selectFBILLNO);
                    //    FBillSurplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码" && e.FBILLNO1 == selectFBILLNO);
                    //}
                }
                _message.Info(aa);
            },
            CancelText = "取消",
            Content = "是否提交？",
        });
        ss = "";
        Batch = "";
        MaterialCode = "";
        Weight = "";
        input.FocusAsync();
    }
    private async Task CustormerSelectChange()
    {
        F_YHKJ_WLGSs.Clear();
    
        var wlList = MaterialList.Where(e => e.FCustomerName == selectCustomerName && e.FEntrynote != "出库检验扫码").GroupBy(e => e.F_YHKJ_WLGS).Select(e => e.First());
        if (wlList != null)
        {
            foreach (var item in wlList)
            {
                F_YHKJ_WLGSs.Add(item);
            }
        }


        selectF_YHKJ_WLGS = F_YHKJ_WLGSs.FirstOrDefault(e => e.FCustomerName == selectCustomerName).F_YHKJ_WLGS;
        //var billNos = MaterialList.Where(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码").GroupBy(e => e.FBILLNO1).Select(e => e.First());
        //if (billNos != null)
        //{
        //    foreach (var item in billNos)
        //    {
        //        FBILLNOs.Add(item);
        //    }
        //}
        var materialList = await cKDServer.GetXSCKDS();
        Amount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS);
        Surplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码");
        //if(selectFBILLNO==null)
        //{
        //    FBillAmount = 0;
        //    FBillSurplus = 0;
        //}
        //else
        //{
        //    FBillAmount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FBILLNO1 == selectFBILLNO);
        //    FBillSurplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码" && e.FBILLNO1 == selectFBILLNO);

        //}
        
    }
    private async Task F_YHKJ_WLGSSelectChange()
    {
        //FBILLNOs.Clear();
        //var billNos = MaterialList.Where(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码").GroupBy(e => e.FBILLNO1).Select(e => e.First());
        //if (billNos != null)
        //{
        //    foreach (var item in billNos)
        //    {
        //        FBILLNOs.Add(item);
        //    }
        //}
        var materialList = await cKDServer.GetXSCKDS();
        Amount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS);
        Surplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码");
        //if(selectFBILLNO==null)
        //{
        //    FBillAmount = 0;
        //    FBillSurplus = 0;
        //}
        //else
        //{
        //    FBillAmount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FBILLNO1 == selectFBILLNO);
        //    FBillSurplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码" && e.FBILLNO1 == selectFBILLNO);

        //}
        
        
    }
    //public async Task FBillSelectChange()
    //{
    //    var materialList = await cKDServer.GetXSCKDS();
    //    FBillAmount = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FBILLNO1 == selectFBILLNO);
    //    FBillSurplus = materialList.Count(e => e.FCustomerName == selectCustomerName && e.F_YHKJ_WLGS == selectF_YHKJ_WLGS && e.FEntrynote != "出库检验扫码" && e.FBILLNO1 == selectFBILLNO);

    //}
}
